package com.xingdata.jjxc.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.xingdata.jjxc.Common;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Base64;

/**
 * SharedPreferences 工具类
 * 
 * @author JiangMin
 * 
 */
public class SharedPreTools {
	private static ArrayList<String> dataList;
	public static SharedPreferences sp(Context context) {
		return context.getSharedPreferences(Common.CNFIGURATION_FILES_NAME, Context.MODE_APPEND);
	}
	
	public static void putBean(Context context, String key, Object obj) {
		if (obj instanceof Serializable) {
			try {
				ByteArrayOutputStream baos = new ByteArrayOutputStream();
				ObjectOutputStream oos = new ObjectOutputStream(baos);
				oos.writeObject(obj);
				String string64 = new String(Base64.encode(baos.toByteArray(),
						0));
				Editor editor = sp(context).edit();
				editor.putString(key, string64);
				editor.commit();
			} catch (IOException e) {
				e.printStackTrace();
			}

		} else {
			throw new IllegalArgumentException(
					"the obj must implement Serializble");
		}

	}

	public static Object getBean(Context context, String key) {
		Object obj = null;
		try {
			String base64 = sp(context).getString(key, "");
			if (base64.equals("")) {
				return null;
			}
			byte[] base64Bytes = Base64.decode(base64.getBytes(), 1);
			ByteArrayInputStream bais = new ByteArrayInputStream(base64Bytes);
			ObjectInputStream ois = new ObjectInputStream(bais);
			obj = ois.readObject();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return obj;
	}
	
	/**
	 * 保存字符串信息到sharedPreference中
	 * 
	 * @param context
	 * @param key
	 * @param value
	 */
	public static void putString(Context context, String key, String value) {
		SharedPreferences sp = context.getSharedPreferences(
				Common.CNFIGURATION_FILES_NAME, Context.MODE_PRIVATE);
		sp.edit().putString(key, value).commit();
	}

	/**
	 * 保存Int信息到sharedPreference中
	 * 
	 * @param context
	 * @param key
	 * @param value
	 */
	public static void putInt(Context context, String key, int value) {
		SharedPreferences sp = context.getSharedPreferences(
				Common.CNFIGURATION_FILES_NAME, Context.MODE_PRIVATE);
		sp.edit().putInt(key, value).commit();
	}
	/**
	 * 保存Boolean信息到sharedPreference中
	 * 
	 * @param context
	 * @param key
	 * @param value
	 */
	public static void putBoolean(Context context, String key, Boolean value) {
		SharedPreferences sp = context.getSharedPreferences(
				Common.CNFIGURATION_FILES_NAME, Context.MODE_PRIVATE);
		sp.edit().putBoolean(key, value).commit();
	}
	
	/**
	 * 获取信息从sharedPreference中
	 * @param context
	 * @param key
	 * @param value
	 */
	public static String getString(Context context, String key, String defvalue) {
		SharedPreferences sp = context.getSharedPreferences(
				Common.CNFIGURATION_FILES_NAME, Context.MODE_PRIVATE);
		String strValue = sp.getString(key, defvalue);

		if (strValue == null || strValue.length() == 0) {
			return "";
		}
		return strValue;
	}

	/**
	 * 获取信息从sharedPreference中
	 * 
	 * @param context
	 * @param key
	 * @param value
	 */
	public static int getInt(Context context, String key, int defvalue) {
		SharedPreferences sp = context.getSharedPreferences(
				Common.CNFIGURATION_FILES_NAME, Context.MODE_PRIVATE);
		int intValue = sp.getInt(key, defvalue);
		return intValue;
	}
	
	
	/**
	 * 获取信息从sharedPreference中
	 * 
	 * @param context
	 * @param key
	 * @param value
	 */
	public static boolean getBoolean(Context context, String key, Boolean defvalue) {
		SharedPreferences sp = context.getSharedPreferences(
				Common.CNFIGURATION_FILES_NAME, Context.MODE_PRIVATE);
		boolean bValue = sp.getBoolean(key, defvalue);
		return bValue;
	}
	/**
	 * 根据key值判断该条记录是否存在
	 * 
	 * @param context
	 * @param key
	 * @return
	 */
	public static Boolean checkSp(Context context, String key) {
		SharedPreferences sp = context.getSharedPreferences(
				Common.CNFIGURATION_FILES_NAME, Context.MODE_PRIVATE);
		return sp.contains(key);
	}

	/**
	 * 根据key值删除某条记录
	 * 
	 * @param context
	 * @param key
	 * @return
	 */
	public static Boolean remove(Context context, String key) {
		SharedPreferences sp = context.getSharedPreferences(
				Common.CNFIGURATION_FILES_NAME, Context.MODE_PRIVATE);
		if (sp.contains(key)) {
			sp.edit().remove(key).commit();
		}
		return true;
	}

	/**
	 * 清除本地SharedPreferences中缓存
	 * 
	 * @param context
	 * @param key
	 * @return
	 */
	public static Boolean clear(Context context) {
		SharedPreferences sp = context.getSharedPreferences(
				Common.CNFIGURATION_FILES_NAME, Context.MODE_PRIVATE);
		sp.edit().clear().commit();
		return true;
	}

	/**
	 * 读取到列表当中
	 * 
	 * @param context
	 * @param key
	 * @return
	 */
	public static List<String> getFromSpList(Context context, String key) {
		dataList = new ArrayList<String>();
		SharedPreferences sp = context.getSharedPreferences(
				Common.CNFIGURATION_FILES_NAME, Context.MODE_PRIVATE);
		String strValue = sp.getString(key, "");
		if (!"".equals(strValue)) {
			if (strValue.contains(",")) {
				// 先把“,”断开
				String[] users = strValue.split(",");
				int nCount = users.length;
				for (int i = 0; i < nCount; i++) {
					dataList.add(users[i]);
				}
			} else {
				dataList.add(strValue);
			}
		}
		return dataList;
	}

	/**
	 * 保存数据到列表中
	 * 
	 * @param context
	 * @param key
	 * @param value
	 */
	public static void saveToSpList(Context context, String key, String value) {
		if (value == null) {
			return;
		}
		if (dataList == null) {
			dataList = new ArrayList<String>();
		} else if (dataList.size() != 0) {
			dataList.clear();
		}
		if (dataList.size() == 0) {
			dataList.add(value);
		} else {
			dataList.addAll(getFromSpList(context, key));
			for (int i = 0; i < dataList.size(); i++) {
				if (dataList.get(i).equals(value)) {
					dataList.remove(i);
				}
			}
		}

		String strData = "";
		// 最终保存的用户信息
		for (int i = 0; i < dataList.size(); i++) {
			String str = dataList.get(i);
			if (strData == "") {
				strData = str;
			} else {
				strData += ("," + str);
			}
		}
		putString(context, key, strData);
	}

}
